<div id="map_canvas" style="width:100%; height:100%"></div>

<script type="text/javascript">
    var cm;
    function initialize() {

        var initpoint =  google.maps.LatLng(10.7723797, 106.658325);

        var myOptions = {
            zoom: 16,
            center: new google.maps.LatLng(10.7723797, 106.658325),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

//        autocomplete.bindTo('bounds', map);
        var contentString =
            'Di chuyển biểu tượng này đến địa chỉ cần đăng<br/> rồi nhấn OK'
            +'<br/><button  onClick="updatelatlng({rid}, cm.getPosition().lat(), cm.getPosition().lng())">OK</button>';


        var infowindow = new google.maps.InfoWindow({
            content:contentString
        });

        var address = "{address}";
        if( address != ""){
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode( { 'address': address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
//                    map.setCenter(results[0].geometry.location);
                    cm = new google.maps.Marker({
                        map: map,
                        draggable:true,
                        position: results[0].geometry.location,
                        icon:new google.maps.MarkerImage("{__SERVER}/views/images/map/flag-export.png"),
                        title:address
                    });
                    map.setCenter(results[0].geometry.location);
                    infowindow.open(map, cm);
                    google.maps.event.addListener(cm, 'click', function () {
                        infowindow.open(map, cm);
                    });
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }else{
            cm = new google.maps.Marker({
                map:map,
                draggable:true,
                position:new google.maps.LatLng(10.7723797, 106.658325),
                icon:new google.maps.MarkerImage("{__SERVER}/views/images/map/flag-export.png"),
                title:"Trung tâm"
            });
            infowindow.open(map, cm);
            google.maps.event.addListener(cm, 'click', function () {
                infowindow.open(map, cm);
            });
        }
//        updatelatlng(1, cm.getPosition().lat(), cm.getPosition().lng()) ;
        google.maps.event.addListener(autocomplete, 'place_changed', function () {
            infowindow.close();
            var place = autocomplete.getPlace();
            if (place.geometry.viewport) {
                map.fitBounds(place.geometry.viewport);
            } else {
                map.setCenter(place.geometry.location);
                map.setZoom(17);  // Why 17? Because it looks good.
            }

            cm.setPosition(place.geometry.location);
            infowindow.open(map, cm);
        });


    }

    function updatelatlng(rid, lat, lng){
        $.get(
            "{__SERVER}/record/updatecoor?rid="+ rid+"&lat="+lat+"&lng="+lng,
            function (data) {
                //alert(data)  ;
                if({uid} != 0)
                    window.location="{__SERVER}/member/tindadang";
                else
                    window.location="{__SERVER}";
            }
        );
    }
    initialize();


</script>